#coding=utf8
#


import re
import requests
import gevent
from gevent import monkey

monkey.patch_all()

class BaseProxy(object):
    def __init__(self):
        self.request = requests.Session()
        self.headers = {'User-Agent':'Mozilla'}
        self.result = []
        self.page = 10

    def verify(self):
        ret = []
        def ping(url):
            ret.append(url)
        gevent.spawn(ping, self.result)
        gevent.wait(jobs)
        return ret

    def makeurls(self):
        pass

class Xicidaili(BaseProxy):
    def __init__(self):
        self.urls = [
            "http://www.xicidaili.com/nn/",
            "http://www.xicidaili.com/nt/",
            "http://www.xicidaili.com/wn/",
            "http://www.xicidaili.com/wt/",]
        BaseProxy.__init(self)

    def callback(url):
        res = self.request.get(url,headers=self.headers).text
        self.res += re.findall(r"<td>(.*?)</td>",res)

    def run(self):
        for page in range(1,10):
            for url in urls:
                gevent.spawn(recv,'%s%s'%(url,page))
        gevent.wait()
        while res:
            d = res[:5]
            res = res[5:]
            print(d)
            '''p={'m':d[2],'h':d[0],'p':d[1]}
            if p not in self.result:
                self.result.append(p)'''

    def www_kuaidaili_com(self):
        res=[]
        urls = [
            "http://www.kuaidaili.com/free/inha/",
            "http://www.kuaidaili.com/free/intr/",
            "http://www.kuaidaili.com/free/outha/",
            "http://www.kuaidaili.com/free/outtr/",]
        for page in range(1,10):
            for url in urls:
                req = self.request.get('%s%s'%(url,page),headers=self.headers)
                res += re.findall(r"<td data-title=\".*?\">(.*?)</td>",req.text)
        while res:
            d = res[:7]
            res = res[7:]
            p={'m':d[3],'h':d[0],'p':d[1]}
            if p not in self.result:
                self.result.append(p)

p=Proxy()
with open('./proxies.proxy','w') as f:
    for d in p.result:
        f.write('%s://%s:%s\n'%(d['m'],d['h'],d['p']))
    f.close()

